package com.example.windowdemo.base;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

/**
 * @author: njb
 * @date: 2023/8/3 15:31
 * @desc:
 */
public abstract class BaseWindow<T> {
    private Context context;
    private boolean isFullScreen;
    private boolean hasTitleBar;
    private int width;
    private int height;
    private T data;
    private static final String TAG = "WindowLog";
    public WindowManager.LayoutParams layoutParams;
    public View contentView;

    public BaseWindow(Context context) {
        this.context = context;
        this.layoutParams = new WindowManager.LayoutParams();
        initView();
    }

    private void initView() {

    }

    public void setFullScreen(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
        if (isFullScreen) {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags &= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setTitleBar(boolean hasTitleBar) {
        this.hasTitleBar = hasTitleBar;
        if (hasTitleBar) {
            layoutParams.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
        layoutParams.width = width;
        layoutParams.height = height;
    }

    public void setPosition(int x, int y) {
        layoutParams.x = x;
        layoutParams.y = y;
    }

    public void setGravity(int gravity) {
        layoutParams.gravity = gravity;
    }

    public void setData(T data){
        this.data = data;
        Log.d(TAG,"===获取到的数据为==="+ data.toString());
    }

    public void addView(View view) {
        this.contentView = view;
    }

    public void setContentView(View view) {
        this.contentView = view;
    }

    protected boolean autoRegisterEventBus() {
        return false;
    }

    /**
     * 模板方法，定义了窗口的显示流程
     */
    public void show() {
        prepare();
        showWindow();
    }

    /**
     * 显示窗口前的准备工作，可以由子类具体实现
     */
    protected void prepare() {

    }

    /**
     * 具体实现窗口的显示操作，由子类具体实现
     */
    protected abstract void showWindow();

    /**
     * 具体实现窗口的销毁操作，由子类具体实现
     */
    protected abstract void hideWindow();

    /**
     * 销毁窗口后的清理工作，可以由子类具体实现
     */
    protected void cleanup() {

    }

    /**
     * 窗口销毁操作，包括窗口的销毁和清理工作
     */
    protected void destroy() {
        hideWindow();
        cleanup();
    }

    /**
     * 视图分离
     */
    public void onDetachedFromWindow() {

    }
}
